new Vue({
    el: '#vue',
    data: {
		action: getRequestValue('action'),
		id: getRequestValue('id'),
		roleData: [],
        sysData: []
    },
    filters: {
    },
    mounted: function () {
    	if(this.action != 'insert') {
        	this.initList();
    	}
        this.validate();
        this.roleList();
    },
    methods: {
        //上传图片
        PicUpload: function (e, num) {
            var that = this;
            var fileData = e.target.files[0];//上传的文件

            // 通过canvas压缩图片
            //结束
            if ((fileData.type === 'image/jpeg' || fileData.type === 'image/png') && fileData.size < 2097152) {
                var fd = new FormData();
                fd.append("file", fileData);
                
                $.ajax({
                    headers: {
                        Authorization: localStorage.getItem("token_admin")
                    },
                    url: AJAX_URL_USER + 'api/sys/file/upload',
                    type: 'post',
                    processData: false,
                    contentType: false,
                    data: fd,
                    success: function (data) {
                        that.sysData.headimgurl = data.data.fileUrl;
                    }
                });
            } else {
                layer.msg('图片必须为jpg,png格式,且图片大小必须小于2MB');
            }
        },
        //删除图片
        DelPic: function (key) {
            layer.msg('图片已删除');
        },
        initList: function () {
            var that = this;

            _AJAX({
                url: AJAX_URL_USER + 'api/sys/sysUser/select/'+that.id,
				type: 'get',
                success: function (data) {
                    that.sysData = data.data;
                }
            });
        },
        roleList: function() {
            var that = this;
			_AJAX({
				url: AJAX_URL_USER + 'api/sys/sysRole/list?page=-1&limit=-1',
				type: 'get',
				success: function(data) {
					that.roleData = data.data.list;
				}
			});
        },
        validate: function() {
            var that = this;
			var form = $('#slifeForm');
			form.validate({
				errorElement: 'span',
				focusInvalid: true,
				messages: {
				},
				rules: {
					userName: {
						maxlength: 64,
						required: true
					},
					nickname: {
						required: true
					},
					state: {
						required: true
					}
				},
				submitHandler: function(form) {
					that.submitData();
				}
			});
        },
        submitData: function() {
            var that = this;
            var url = "";
            var roleList = [that.sysData.roleId];
            
            var data = {
				userName: that.sysData.userName,
				password: that.sysData.password,
				phone: that.sysData.phone,
				state: that.sysData.state,
				roleList: roleList,
				headimgurl: that.sysData.headimgurl,
				nickname: that.sysData.nickname
            }

            if(that.action == "insert") {
            	url = AJAX_URL_USER + 'api/sys/sysUser/insert';
            }
            if(that.action == "update") {
            	url = AJAX_URL_USER + 'api/sys/sysUser/update';
            	data.id = that.id;
            }
            
			_AJAX({
				url: url,
				type: 'post',
				data: JSON.stringify(data),
				success: function(data) {
	    			var index = parent.layer.getFrameIndex(window.name);
					parent.layer.close(index);
				}
			});
        }
    }
});




